/**
 * Copyright 2015 Qiyi Inc. All rights reserved.
 *
 * @file:   Program.js
 * @path:   js-src/album/
 * @author: karkarrotxu@qiyi.com
 * @date:   2016-03-01
 */
qui(
    function (require, exports, module) {
        Program.Task = $.extend({}, EpgWeb.Task, {
            getLiveProgramType: function (taskName , handler) {
                var self = this;
                if(handler == null ){
                    handler =  function (data) {
                        var selectJDom = $("select[name=liveProgramType]");
                        $("<option value=''>请选择</option>").appendTo(selectJDom);
                        if (data.data != null) {
                            $.each(data.data, function (index, obj) {
                                $("<option>", {
                                    value: obj.key,
                                    html: obj.descr
                                }).appendTo(selectJDom);
                            });
                        }
                        selectJDom.bind("change", function (event) {
                            if (event.currentTarget.value == 1) {
                                $("#sportContainer").show()
                            } else {
                                $("#sportContainer").hide()
                            }
                        })
                    }
                }
                return self.getConfigType(taskName,handler);
            },
            getLegoChannelType: function (taskName) {
                var self = this;
                return self.getConfigType(taskName, function (data) {
                    var selectJDom = $("#channelCateTag");
                    $.each(data.data, function (index, obj) {
                        $("<option>", {
                            value: obj.key,
                            html: obj.val
                        }).appendTo(selectJDom);
                    });
                })
            },
            getTargetName: function (regName, data) {
                return {
                    regName: regName,
                    options: {
                        type: "post",
                        url: EpgWeb.urlConfig.programList,
                        dataType: "json",
                        data: data
                    }
                }
            },

            getLiveTmpOtherType: function (regName, data) {
                var self = this;
                return self.getConfigType(regName, function (data) {
                    var selectJDom = $("#" + regName);
                    $.each(data.data, function (index, obj) {
                        $("<option>", {
                            value: obj.val,
                            html: obj.descr
                        }).appendTo(selectJDom);
                    });
                })
            },

            getLiveChannelType: function (taskName) {
                var self = this;
                return self.getConfigType(taskName)
            },

            getLogType:function(taskName){
                var self = this;
                return self.getConfigType(taskName)
            },
            getLiveType: function (taskName) {
                var self = this;
                return self.getConfigType(taskName, function (data) {
                    var selectJDom = $("select[name=liveType]");
                    var selectChild = $("select[name=liveChannelType]");
                    selectChild.hide();
                    if (data.data == null) {
                        return
                    }
                    $.each(data.data, function (index, obj) {
                        $("<option>", {
                            value: obj.key,
                            html: obj.descr
                        }).appendTo(selectJDom);
                    });
                    selectJDom.bind("change", function (event) {
                        selectChild.show();
                        selectChild.html("");
                        var appendChildren = [];
                        $.each(Program.Dao.liveChannelType().data, function (index, obj) {
                            if (Program.Util.dispatchLiveChannelType(obj.key, event.currentTarget.value)) {
                                appendChildren.push($("<option>", {
                                    value: obj.key,
                                    html: obj.val
                                }));
                            }
                        });
                        if (appendChildren.length == 0) {
                            selectChild.hide();
                        } else {
                            appendChildren = [$("<option value=''>请选择</option>")].concat(appendChildren)
                        }
                        $.each(appendChildren, function (i, element) {
                            element.appendTo(selectChild);
                        })
                    })
                })
            },
            sendTask: function (modelData, sendImageDatas) {
                return {
                    regName: null,
                    options: {
                        type: "post",
                        dataType: "json",
                        data: {"data": JSON.stringify(modelData), "imageDatas": sendImageDatas == null ? null : JSON.stringify(sendImageDatas) },
                        success: function (data) {
                            //   callback
                        },
                        error: function (data) {

                        }
                    }
                }
            },

            getGradeType: function (taskName) {
                var self = this;
                return self.getConfigType(taskName, function (data) {
                    var selectJDom = $("select[name=episodeGrade]");
                    $("<option>", {value: "", html: "请选择"}).appendTo(selectJDom);
                    $.each(data.data, function (index, obj) {
                        $("<option>", {
                            value: obj.key,
                            html: obj.descr
                        }).appendTo(selectJDom);
                    });
                })
            },
            getRecommendType: function (taskName) {
                var self = this;
                return self.getConfigType(taskName, function (data) {
                    var selectJDom = $("select[name=recommend_rate]");
                    $("<option>", {value: "", html: "请选择"}).appendTo(selectJDom);
                    $.each(data.data, function (index, obj) {
                        $("<option>", {
                            value: obj.key,
                            html: obj.descr
                        }).appendTo(selectJDom);
                    });
                })
            }


        })
    }
);